package com.chooongg.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RestrictTo
import androidx.recyclerview.widget.RecyclerView
import com.chooongg.adapter.listener.*
import com.chooongg.adapter.module.*
import com.chooongg.adapter.viewHolder.BoxViewHolder
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView

abstract class BoxAdapter<T, HOLDER : BoxViewHolder>(data: MutableList<T>? = null) :
    RecyclerView.Adapter<HOLDER>(), ImpBoxAdapterModule, ImpBaseListener {

    companion object {
        const val ITEM_TYPE_LOAD_MORE = 9999
        const val ITEM_TYPE_HEADER_BEGIN = 10000
        const val ITEM_TYPE_FOOTER_BEGIN = 20000
    }

    /**
     * 数据
     */
    var data: MutableList<T> = data ?: mutableListOf()
        internal set

    /**
     * headerView 是否单行显示
     */
    var headerViewAsSingleColumn = true

    /**
     * footerView 是否单行显示
     */
    var footerViewAsSingleColumn = true

    /**
     * 是否启用动画
     */
    var animationEnable = false

    /**
     * 动画是否仅第一次执行
     */
    var animationFirstOnly = true

//    val loadMoreModule: BoxLoadMoreModule
//        get() {
//
//        }

    private val headerViews = ArrayList<View>()
    private val footerViews = ArrayList<View>()

    private var lastPosition = -1

    private var spanSizeLookup: GridSpanSizeLookup? = null
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    private var mOnItemChildClickListener: OnItemChildClickListener? = null
    private var mOnItemChildLongClickListener: OnItemChildLongClickListener? = null
    private var upFetchModule: BoxUpFetchModule? = null
    private var draggableModule: BoxDraggableModule? = null
    internal var loadMoreModule: BoxLoadMoreModule? = null

    protected lateinit var context: Context
        private set

    internal var mRecyclerView: RecyclerView? = null

    var recyclerView: RecyclerView
        set(value) {
            mRecyclerView = value
        }
        get() {
            checkNotNull(mRecyclerView) {
                "Please get it after onAttachedToRecyclerView()"
            }
            return mRecyclerView!!
        }

    init {
        checkModule()
    }

    /**
     * 检查模块
     */
    private fun checkModule() {
        if (this is LoadMoreModule) {
            loadMoreModule = this.addLoadMoreModule(this)
        }
        if (this is UpFetchModule) {
            upFetchModule = this.addUpFetchModule(this)
        }
        if (this is DraggableModule) {
            draggableModule = this.addDraggableModule(this)
        }
    }

    protected abstract fun convert(holder: HOLDER, item: T)

    protected open fun convert(holder: HOLDER, item: T, payloads: List<Any>) = Unit

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HOLDER {

        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        return headerViews.size + data.size + footerViews.size + (if (loadMoreModule?.hasLoadMoreView() == true) 1 else 0)
    }

    @RestrictTo(RestrictTo.Scope.LIBRARY)
    override fun getItemViewType(position: Int) = when {
        position < headerViews.size -> {
            ITEM_TYPE_HEADER_BEGIN + position
        }
        position < headerViews.size + data.size -> {
            getDefaultItemViewType(position - headerViews.size)
        }
        position < headerViews.size + data.size + footerViews.size -> {
            ITEM_TYPE_FOOTER_BEGIN + position - headerViews.size - data.size
        }
        else -> ITEM_TYPE_LOAD_MORE
    }

    protected open fun getDefaultItemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

}